package com.xhs.zdl.demo.treeNode;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.google.gson.Gson;
import com.xhs.zdl.demo.R;
import com.xhs.zdl.demo.tools.MyNormalUtils;
import com.xhs.zdl.demo.tools.MyStringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TreeNodeActivity extends AppCompatActivity {

    private Activity activity = TreeNodeActivity.this;
    private ListView listView;

    private List<TreeNodeBean> data;
    private TreeNodeAdapter adapter;

    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tree_node);

        initView();
    }

    private void initView() {
        listView = (ListView) findViewById(R.id.treeListView);

        gson = new Gson();

        data = new ArrayList<>();
        data.add(new TreeNodeBean(-1, "安装配置", new ArrayList<TreeNodeBean>()));
        adapter = new TreeNodeAdapter(this, data);
        listView.setAdapter(adapter);
        adapter.setClickListener(addOrMinusClickListener);
    }

    AddOrMinusClickListener addOrMinusClickListener = new AddOrMinusClickListener() {
        @Override
        public void addNode(int level, int position) {
            List<TreeNodeBean> tempList = data.subList(0, position+1);
            tempList.add(new TreeNodeBean(level, "这是一条数据", new ArrayList<TreeNodeBean>()));
            adapter.notifyDataSetChanged();
        }

        @Override
        public void minusNode(int level, int position) {
            int nextIndex = data.size();
            for (int i = 0; i < data.size(); i++) {
                if (i > position && data.get(i).getLevel() == level){
                    nextIndex = i;
                }
            }
            List<TreeNodeBean> delData = data.subList(position, nextIndex);
            data.removeAll(delData);
            adapter.notifyDataSetChanged();
        }
    };

    private Map<String, TreeNodeBean> parentBean = new HashMap<>();
    private String json = "";
    public void confirm(View view){
        for (int i = 0; i < data.size(); i++) {
            TreeNodeBean treeNodeBean = data.get(i);
            parentBean.put(treeNodeBean.getLevel()+"", treeNodeBean);
            if (i != data.size()-1){
                if (treeNodeBean.getLevel() < data.get(i+1).getLevel()){
                    //当前这个是下一个的父集
                    parentBean.get(data.get(i+1).getLevel()-1+"").getBean().add(data.get(i+1));
                }else if (treeNodeBean.getLevel() == data.get(i+1).getLevel()){
                    //当前这个与下一个同级
                    parentBean.get(data.get(i+1).getLevel()-1+"").getBean().add(data.get(i+1));
                }else if (treeNodeBean.getLevel() > data.get(i+1).getLevel()){
                    //当前这个与下一个没有关系
                    parentBean.get(data.get(i+1).getLevel()-1+"").getBean().add(data.get(i+1));
                }
            }
        }
        TreeNodeBean bean = data.get(0);
        json = getJson(bean, json);
        json = json.substring(0, json.length()-1);
        MyNormalUtils.mLog("************", json);
    }

    private String getJson(TreeNodeBean bean, String json){
        if (bean.getBean().size() != 0){
            if (MyStringUtils.isEmpty(json)){
                json += "{\"config\":[";
            }else {
                json += "{\"" + bean.getInfo() + "\":[";
            }

            for (int i = 0; i < bean.getBean().size(); i++) {
                TreeNodeBean childBean = bean.getBean().get(i);
                if (childBean.getBean().size() != 0){
                    json = getJson(childBean, json);
                }else {
                    json += "\"" + childBean.getInfo() + "\",";
                }
            }
            // 去除最后一个
            json = json.substring(0, json.length() - 1);
            json += "]},";
        }
        return json;
    }
}
/*
List<TreeNodeBean> tempList = data.subList(0, position);
            tempList.add(new TreeNodeBean(level, "level：" + level + "，position：" + position));
            MyNormalUtils.mLog("**********", gson.toJson(data));
            adapter.notifyDataSetChanged();

int nextIndex = data.size();
            List<TreeNodeBean> tempBean = new ArrayList<>();
            for (int i = 0; i < data.size(); i++) {
                TreeNodeBean treeNodeBean = data.get(i);
                if (treeNodeBean.getLevel() == level){
                    tempBean.add(treeNodeBean);
                }
            }
            for (int i = 0; i < tempBean.size(); i++) {
                if (data.get(position).getInfo().equals(tempBean.get(i).getInfo()) && i != tempBean.size()-1){
                    nextIndex = data.indexOf(tempBean.get(i+1));
                }
            }
            List<TreeNodeBean> delData = data.subList(position, nextIndex);
            data.removeAll(delData);
            adapter.notifyDataSetChanged();
 */